package com.hyzx.qbasic.admin.controller.system;

import com.github.pagehelper.PageInfo;
import com.hyzx.qbasic.admin.model.constant.PermissionConst;
import com.hyzx.qbasic.admin.controller.BaseController;
import com.hyzx.qbasic.admin.model.annotation.SysLog;
import com.hyzx.qbasic.admin.model.dto.KeyValuePair;
import com.hyzx.qbasic.admin.model.po.SysParamPO;
import com.hyzx.qbasic.admin.model.vo.req.admin.*;
import com.hyzx.qbasic.admin.model.vo.res.BaseOperationRes;
import com.hyzx.qbasic.admin.model.vo.res.BaseResModel;
import com.hyzx.qbasic.admin.common.util.BaseResUtils;
import com.hyzx.qbasic.admin.domain.service.ISysParamService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

/**
 * Created by 黄江华 on 2018/7/20
 */


@Api(tags = "数据字典", description = "SysParamController")
@Controller
@RequestMapping("/sys/param")
public class SysParamController extends BaseController {

    @Autowired
    private ISysParamService paramService;

    @ApiIgnore
    @SysLog("数据字典首页")
    @GetMapping("/list")
    public String list(Model model) {
        model.addAttribute("addFunc", getFunction(PermissionConst.SYS_PARAM_ADD));
        model.addAttribute("editFunc", getFunction(PermissionConst.SYS_PARAM_EDIT));
        model.addAttribute("deleteFunc", getFunction(PermissionConst.SYS_PARAM_DELETE));
        return "system/param/list";
    }

    @ApiOperation("获取数据字典列表")
    @GetMapping("/getParamList")
    @ResponseBody
    public BaseResModel<PageInfo<SysParamPO>> getParamList(@Valid GetParamListReq req, BindingResult result) {
        return BaseResUtils.success(paramService.getPagedParamList(req));
    }

    @ApiIgnore
    @SysLog("新增数据字典页")
    @GetMapping("/add")
    public String add() {
        return "system/param/add";
    }

    @ApiOperation("新增数据字典表单提交")
    @PostMapping("/add")
    @ResponseBody
    public BaseResModel<BaseOperationRes> add(@Valid @RequestBody AddParamReq req, BindingResult result) {
        BaseOperationRes res = paramService.addParam(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("删除数据字典")
    @PostMapping("/delete")
    @ResponseBody
    public BaseResModel<BaseOperationRes> delete(@Valid @RequestBody DeleteParamReq req, BindingResult result) {
        BaseOperationRes res = paramService.deleteParam(req);
        return BaseResUtils.success(res);
    }

    @ApiIgnore
    @SysLog("编辑数据字典页")
    @GetMapping("/edit")
    public String edit() {
        return "system/param/edit";
    }

    @ApiOperation("编辑数据字典表单提交")
    @PostMapping("/edit")
    @ResponseBody
    public BaseResModel<BaseOperationRes> edit(@Valid @RequestBody EditParamReq req, BindingResult result) {
        BaseOperationRes res = paramService.editParam(req);
        return BaseResUtils.success(res);
    }

    @ApiOperation("获取数据字典信息")
    @GetMapping("/getParamInfo")
    @ResponseBody
    public BaseResModel<SysParamPO> getParamInfo(@Valid GetParamInfoReq req, BindingResult result) {
        return BaseResUtils.success(paramService.getParamInfo(req.getpId()));
    }

    @ApiOperation("根据字典类型获取数据字典列表")
    @GetMapping("/getParamListByType")
    @ResponseBody
    public BaseResModel<List<KeyValuePair>> getParamListByType(@Valid GetParamListByTypeReq req, BindingResult result) {
        return BaseResUtils.success(paramService.getParamListByType(req.getType()));
    }
}
